/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.slice.edittext;

import com.xuexiang.xui_lib.component.edittext.VerifyCodeEditText;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

/**
 * 自定义方格输入
 *
 * @since 2021-04-28
 */
public class VeriftEditAbilitySlice extends AbilitySlice implements VerifyCodeEditText.OnInputListener {
    private VerifyCodeEditText vcet1;
    private VerifyCodeEditText vcet2;
    private Button btnClear;
    private VerifyCodeEditText vcet3;
    private VerifyCodeEditText vcet4;
    private Component backImage;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_verifycode_edittext);
        init();
    }

    /**
     * 初始化
     */
    public void init() {
        vcet1 = (VerifyCodeEditText) findComponentById(ResourceTable.Id_vcet_1);
        vcet2 = (VerifyCodeEditText) findComponentById(ResourceTable.Id_vcet_2);
        vcet3 = (VerifyCodeEditText) findComponentById(ResourceTable.Id_vcet_3);
        vcet4 = (VerifyCodeEditText) findComponentById(ResourceTable.Id_vcet_4);
        vcet1.setOnInputListener(this);
        vcet2.setOnInputListener(this);
        vcet3.setOnInputListener(this);
        vcet4.setOnInputListener(this);
        backImage = findComponentById(ResourceTable.Id_backImage);
        backImage.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
        btnClear = (Button) findComponentById(ResourceTable.Id_btn_clear);
        btnClear.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                vcet1.clearInputValue();
                vcet2.clearInputValue();
                vcet3.clearInputValue();
                vcet4.clearInputValue();
            }
        });
    }

    @Override
    public void onComplete(String input) {
        ToastUtil.showToast(getContext(), "onComplete:" + input);
    }

    @Override
    public void onChange(String input) {
        ToastUtil.showToast(getContext(), "onChange:" + input);
    }

    @Override
    public void onClear() {
        ToastUtil.showToast(getContext(), "onClear:");
    }
}